(function() {
	$('#time-format3').on('click', function() {
		var startTime = sessionStorage.getItem('starttime');
		if (startTime) {
			$('.mask2').css({
				display: 'block'
			});
			$('#timePicker2').css({
				display: 'block'
			});
			Timeconfirm(startTime);
		} else {
			alert('还未选择出发时间');
		}
	});

	function Timeconfirm(startTime) {
		function getWeek(week) {
			switch (week) {
				case 0:
					return '周日';
					break;
				case 1:
					return '周一';
					break;
				case 2:
					return '周二';
					break;
				case 3:
					return '周三';
					break;
				case 4:
					return '周四';
					break;
				case 5:
					return '周五';
					break;
				case 6:
					return '周六';
					break;
			}
		}
		//参数值小于10在前面添加0
		function addZero(value) {
			if (value < 10) {
				return '0' + value;
			} else {
				return value;
			}
		}
		//获取指定月份间隔
		function monthScope(value1, month) {
			var getMonth = function(str) {
				var list = str.split('-');
				var year = list[0];
				var month = parseInt(list[1]) - 1;

				var tempDate = new Date(year, month, 1);
				return tempDate;
			};
			var month1 = getMonth(value1);

			var nextMonth = new Date();
			nextMonth.setDate(1);
			nextMonth.setFullYear(month1.getFullYear());
			nextMonth.setMonth(month1.getMonth() + month);
			var month2 = nextMonth;
			var monthArr = [];
			var i = 0;
			while (!(month1.getFullYear() == month2.getFullYear() && month1.getMonth() == month2.getMonth())) {
				monthArr[i] = month1.getFullYear() + '-' + (month1.getMonth() + 1);

				i++;

				month1.setMonth(month1.getMonth() + 1);
			}
			return monthArr;
		}

		function addMonth() {
			var monthStr = '';
			for (var i = 0; i < monthList.length; i++) {
				monthStr += '<li>' + monthList[i].split('-')[1] + '月</li>';
			}
			$('#pickMonth2 ul').html(monthStr);
		}

		//获取从当前日期到指定间隔几个月的所有日期
		function currentDateScope(value1) {
			var list = value1.split('-');
			var currentDate = new Date(list[0], list[1] - 1, list[2]);

			var endDate = new Date();
			endDate.setFullYear(currentDate.getFullYear());
			endDate.setDate(1);
			endDate.setMonth(currentDate.getMonth() + 1);

			var currentDateArr = [];
			var i = 0;
			while (
				!(
					currentDate.getFullYear() == endDate.getFullYear() &&
					currentDate.getMonth() == endDate.getMonth() &&
					currentDate.getDate() == endDate.getDate()
				)
			) {
				currentDateArr[i] =
					currentDate.getFullYear() +
					'-' +
					(currentDate.getMonth() + 1) +
					'-' +
					currentDate.getDate() +
					'-' +
					currentDate.getDay();
				i++;
				currentDate.setDate(currentDate.getDate() + 1);
			}
			return currentDateArr;
		}

		function addDate() {
			var dateStr = '';
			for (var i = 0; i < dateList.length; i++) {
				var list = dateList[i].split('-');
				dateStr += '<li>' + list[2] + '日' + getWeek(parseInt(list[3])) + '</li>';
			}
			$('#pickDate2 ul').html(dateStr);
		}

		//获取指定间隔时间的所有时间
		function timeScope(startTime, endTime) {
			var getTime = function(str) {
				var tempTime = new Date();
				var list = str.split(':');
				tempTime.setHours(list[0]);
				tempTime.setMinutes(list[1]);
				return tempTime;
			};
			var time1 = getTime(startTime);
			var time2 = getTime(endTime);
			if (time1 > time2) {
				var tempDate = time1;
				time1 = time2;
				time2 = tempDate;
			}
			var timeArr = [];
			var i = 0;
			while (!(time1.getHours() == time2.getHours() && time1.getMinutes() == time2.getMinutes())) {
				var hourStr = time1.getHours().toString();
				var minuteStr = time1.getMinutes().toString();
				if (hourStr.length == 1) {
					hourStr = '0' + hourStr;
				}
				if (minuteStr.length == 1) {
					minuteStr = '0' + minuteStr;
				}
				timeArr[i] = hourStr + ':' + minuteStr;
				i++;
				time1.setMinutes(time1.getMinutes() + 30);
			}
			timeArr.push(endTime);
			return timeArr;
		}

		function addTime() {
			var timeStr = '';
			for (var i = 0; i < timeList.length; i++) {
				timeStr += '<li>' + timeList[i] + '</li>';
			}
			$('#pickTime2 ul').html(timeStr);
		}

		var timeList = [];
		var frTimeList = [];
		var dateList = [];
		var frDateList = [];
		var monthList = [];

		var arr = startTime.split(' ');
		var startDateArr = arr[0].split('-');
		var startTimeArr = arr[1].split(':');
		var start = new Date(startDateArr[0], startDateArr[1], startDateArr[2]);
		start.setHours(startTimeArr[0]);
		start.setMinutes(parseInt(startTimeArr[1]) + 60);

		var startTimeStr = start.getHours() + ':' + start.getMinutes();
		timeList = timeScope(startTimeStr, '23:30');
		frTimeList = timeScope(startTimeStr, '23:30');
		addTime();
		$('#pickTime2 ul li').eq(0).addClass('selected');

		var startDateStr = start.getFullYear() + '-' + start.getMonth() + '-' + start.getDate();
		dateList = currentDateScope(startDateStr);

		frDateList = currentDateScope(startDateStr);
		addDate();
		$('#pickDate2 ul li').eq(0).addClass('selected');
		var startMonthStr = start.getFullYear() + '-' + start.getMonth();
		monthList = monthScope(startMonthStr, 2);
		addMonth();
		$('#pickMonth2 ul li').eq(0).addClass('selected');

		//滚动变化部分逻辑
		var myScrollMonth = new IScroll('#pickMonth2', {
			probeType: 3,
			deceleration: 0.001,
			bounce: false
		});
		var myScrollDate = new IScroll('#pickDate2', {
			probeType: 3,
			deceleration: 0.001,
			bounce: false
		});
		var myScrollTime = new IScroll('#pickTime2', {
			probeType: 3,
			deceleration: 0.001,
			bounce: false
		});

		myScrollMonth.on('scroll', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			$('#pickMonth2 ul li').eq(Math.abs(y)).addClass('selected').siblings().removeClass('selected');
		});
		myScrollMonth.on('scrollEnd', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			$('#pickMonth ul').css({
				transform: 'translate(0,' + 1 * y + 'rem)',
				transition: '0.1s all'
			});
			if (Math.abs(y) != 0) {
				timeList = timeScope('00:00', '23:30');
				addTime();
				$('#pickTime2 ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickTime2 ul li').eq(0).addClass('selected');
				myScrollTime.refresh();
				dateList = currentDateScope(monthList[Math.abs(y)] + '-' + 1);
				addDate();
				$('#pickDate2 ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickDate2 ul li').eq(0).addClass('selected');
				myScrollDate.refresh();
			} else {
				timeList = [];
				timeList = frTimeList;
				addTime();
				$('#pickTime2 ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickTime2 ul li').eq(0).addClass('selected');
				myScrollTime.refresh();
				dateList = [];
				dateList = frDateList;
				addDate();
				$('#pickDate2 ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickDate2 ul li').eq(0).addClass('selected');
				myScrollDate.refresh();
			}
		});

		myScrollDate.on('scroll', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			$('#pickDate2 ul li').eq(Math.abs(y)).addClass('selected').siblings().removeClass('selected');
		});
		myScrollDate.on('scrollEnd', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			$('#pickDate2 ul').css({
				transform: 'translate(0,' + 1 * y + 'rem)',
				transition: '0.1s all'
			});
			if (Math.abs(y) != 0) {
				timeList = timeScope('00:00', '23:30');
				addTime();
				$('#pickTime2 ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickTime2 ul li').eq(0).addClass('selected');
				myScrollTime.refresh();
			} else {
				timeList = [];
				timeList = frTimeList;
				addTime();
				$('#pickTime2 ul').css({
					transform: 'translate(0,0)',
					transition: '0.1s all'
				});
				$('#pickTime2 ul li').eq(0).addClass('selected');
				myScrollTime.refresh();
			}
		});

		myScrollTime.on('scroll', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			$('#pickTime2 ul li').eq(Math.abs(y)).addClass('selected').siblings().removeClass('selected');
		});
		myScrollTime.on('scrollEnd', function() {
			var fontSize = parseInt(document.documentElement.style.fontSize);
			var y = Math.round(this.y / fontSize / 1);
			if (Math.abs(y) < $('#pickTime2 ul li').length) {
				$('#pickTime2 ul').css({
					transform: 'translate(0,' + 1 * y + 'rem)',
					transition: '0.1s all'
				});
			}
		});

		$('#timePicker2 .confirm2').on('click', function() {
			var dateStr = dateList[$('#pickDate2 ul li.selected').index()];
			var timeStr = timeList[$('#pickTime2 ul li.selected').index()];
			var arr = dateStr.split('-');
			var dateDay = addZero(arr[1]) + '月' + addZero(arr[2]) + '日';
			var goTime = addZero(arr[0]) + '-' + addZero(arr[1]) + '-' + addZero(arr[2]) + ' ' + timeStr;
			sessionStorage.setItem('endTime', goTime);
			$('#time-format3 .Pickup_date').text(dateDay);
			$('#time-format3 .Pickup_times>.timer').text(timeStr);
			timess();

			$('#timePicker2').css({
				display: 'none'
			});
			$('.mask2').css({
				display: 'none'
			});
		});
		$('#timePicker2 .cancel2').on('click', function() {
			$('#timePicker2').css({
				display: 'none'
			});
			$('.mask2').css({
				display: 'none'
			});
		});
		$('.mask2').on('click', function() {
			$('#timePicker2').css({
				display: 'none'
			});
			$('.mask2').css({
				display: 'none'
			});
		});
		function timess() {
			var Daydatas = sessionStorage.getItem('endTime');
			sDate1 = startTime;
			sDate2 = Daydatas;
			// console.log(sDate1, sDate2)
			var Days = DateDiff(sDate1, sDate2);
			$('.Content_module ul li.times .mid span em').text(Days);
		}
	}
	//计算天数差的函数，通用
	function DateDiff(sDate1, sDate2) {
		//sDate1和sDate2是xxxx-xx-xx格式
		/**
     * @starttime: 开始时间
     * @endtime：  结束时间
     * @differ：   差值
     */
		var starttime, endtime, differ;

		starttime = new Date(sDate1.replace(/-/g, '/'));
		endtime = new Date(sDate2.replace(/-/g, '/'));
		differ = (endtime - starttime) / (1000 * 60 * 60); //相差的小时
		if (differ >= 24) {
			//大于24小时
			differ = Math.ceil(differ / 24); //向上取整---这里比较关键
			return differ;
		} else {
			return 1;
		}
	}
})();
